package com.kejiguan.ui.presenter;

import android.widget.Toast;

import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.GetNeItemInfosResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by YYY on 16/9/2.
 */
public class AlarmActivityPresenter extends BaseActivityPresenter {

    public void getNeItemInfos() {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "703");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        final GsonRequest<GetNeItemInfosResponse> request = new GsonRequest("getNeItemInfos", GetNeItemInfosResponse.class, new Response.Listener<GetNeItemInfosResponse>() {
            @Override
            public void onResponse(GetNeItemInfosResponse response) {

                if ("0000".equals(response.getRetCode())&&null!=response.getExhibitionRecordMap()) {

                    Set<String> keys=response.getExhibitionRecordMap().keySet();
                    Iterator<String> stringIterator=keys.iterator();
                    while (stringIterator.hasNext())
                    {
                        String key=stringIterator.next();
                        String[] mutyKeys=key.split("=");

                        if (CacheDataUtil.getInstance().getDisPlayAreaIdNames().contains(key))
                        {

                            int disPalyAreaCount=CacheDataUtil.getInstance().getDisplayAreas().size();
                            for (int i=0;i<disPalyAreaCount;i++)
                            {
                                if (mutyKeys[0].equals(CacheDataUtil.getInstance().getDisplayAreas().get(i).getDisplayId()))
                                {
                                    Collections.sort(response.getExhibitionRecordMap().get(key));
                                    CacheDataUtil.getInstance().getDisplayAreas().get(i).setExhibitionNeItems(response.getExhibitionRecordMap().get(key));
                                }
                            }

                        }else//新增操作
                        {

                            DisplayArea displayArea=new DisplayArea();
                            displayArea.setDisplayId(mutyKeys[0]);
                            displayArea.setDisplayName(mutyKeys[1]);
                            Collections.sort(response.getExhibitionRecordMap().get(key));
                            displayArea.setExhibitionNeItems(response.getExhibitionRecordMap().get(key));

                            CacheDataUtil.getInstance().getDisplayAreas().add(displayArea);
                            CacheDataUtil.getInstance().getDisPlayAreaIdNames().add(key);
                        }
                    }

//                    List<ExhibitionNeItem> exhibitionNeItems=new ArrayList<>(0);
//
//                    for (int i=0;i<CacheDataUtil.getInstance().getDisplayAreas().size();i++)
//                    {
//                        DisplayArea displayArea= CacheDataUtil.getInstance().getDisplayAreas().get(i);
//                        displayArea.initDatas();
//
//                        List<ExhibitionNeItem> neItems=displayArea.getExhibitionNeItems();
//                        if (null!=neItems)
//                        {
//                            for (int z=0;z<neItems.size();z++)
//                            {
//                                ExhibitionNeItem neItem=neItems.get(z);
//                                if (neItem.getExhibitionState().equals(CommonDate.STATE_EXCEPTION)||
//                                        neItem.getExhibitionState().equals(CommonDate.STATE_OPEN_TIME_OUT)||
//                                        neItem.getExhibitionState().equals(CommonDate.STATE_CLOSE_TIME_OUT))
//                                {
//                                    exhibitionNeItems.add(neItem);
//                                }
//                            }
//                        }
//                    }
//
//                    CacheDataUtil.getInstance().setAlarmExhibitionNeItems(exhibitionNeItems);
                }

                EventBus.getDefault().post(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext, "获取网元信息失败", Toast.LENGTH_SHORT).show();
            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }
}
